package models;
import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import util.DBAction;
import util.MyLogAction;
public class IssueData {
	
	public int getProject_id() {
		return project_id;
	}
	public void setProject_id(int project_id) {
		this.project_id = project_id;
	}
	private int issue_id;
	private int dev_id;
	public int getDev_id() {
		return dev_id;
	}
	public void setDev_id(int dev_id) {
		this.dev_id = dev_id;
	}
	public int getIssue_id() {
		return issue_id;
	}
	public void setIssue_id(int issue_id) {
		this.issue_id = issue_id;
	}
	
	public String getIssue_status() {
		return issue_status;
	}
	public void setIssue_status(String issue_status) {
		this.issue_status = issue_status;
	}
	private int issue_num;
	private int project_id;
	private String issue_status;
	private String issue_subject;
	private String descp;
	private String assignedto;
	private String category;
	private String client_email;
	private String client_phone_no;
	private String tags;
	private int issue_priority;
	public int getIssue_priority() {
		return issue_priority;
	}
	public void setIssue_priority(int issue_priority) {
		this.issue_priority = issue_priority;
	}
	public String getTags() {
		return tags;
	}
	public void setTags(String tags) {
		this.tags = tags;
	}
	private String seq_qn;
	private String seq_ans;
	private String created_datetime;
	public int updateIssue(int issue_number) {
		String updateSQL = 
			    "update issue "
				+ "set issue_subject = '" + issue_subject + 
				"', issue_descp = '" + descp + "', issue_category = '" + category + "'," +
						"client_email = '" + client_email + "', client_phone_no = '" + client_phone_no + "'," +
								" seq_qn = '" + seq_qn + "', seq_ans = '" + seq_ans + "', " +
										"created_datetime = '" + created_datetime + "'where issue_num = "+issue_number+";";
		return DBAction.update(updateSQL);
	}

	public int insert() {
		String insertSQL = "insert into issue "
				+ "(issue_num,issue_subject, issue_descp, issue_category, client_email, client_phone_no, seq_qn, seq_ans, issue_status) " + 
				"values(" + issue_num + ", '" + issue_subject + "', '" + descp + "', '" + category + "', '" + client_email +"', '" + client_phone_no + "', '" + seq_qn + "'" +
						", '" + seq_ans + "','new');";
		return DBAction.update(insertSQL);
	}

	public static ArrayList<IssueData> find(String selectionModifier) {
		ArrayList<IssueData> selection = new ArrayList<IssueData>();
		ResultSet resultSet = null;
		String query = "select * from issue " + selectionModifier; /*"select issue_num, issue_subject, descp, " +
				"assignedto, category, client_email, client_phone_no, " +
				"seq_qn, seq_ans," +
				" created_datetime" +
				"from issue " + selectionModifier;*/
		Connection connection = DBAction.getConnection();
		resultSet = DBAction.readFromDB(query, connection);
		try {
			while (resultSet.next()) {
				IssueData issue = new IssueData();
				issue.issue_id = resultSet.getInt("issue_id");
				
				issue.issue_num = resultSet.getInt("issue_num");
				//System.out.println("issue num in issue data is "+issue.issue_num);
				issue.project_id = resultSet.getInt("project_id");
				issue.issue_subject = resultSet.getString("issue_subject");
				issue.issue_status = resultSet.getString("issue_status");
				issue.descp = resultSet.getString("issue_descp");
				issue.issue_priority=resultSet.getInt("issue_priority");
				issue.category = resultSet.getString("issue_category");
				issue.client_email = resultSet.getString("client_email");
				issue.client_phone_no = resultSet.getString("client_phone_no");
				issue.seq_qn = resultSet.getString("seq_qn");
				issue.seq_ans = resultSet.getString("seq_ans");
				issue.created_datetime = resultSet.getString("created_datetime");
				issue.assignedto = resultSet.getString("assignedto");
				selection.add(issue);
			}
		} catch (SQLException e) {
            MyLogAction.myCatch("issue.java", 43, e);
			e.printStackTrace();
		}
		DBAction.close(resultSet);
		DBAction.close(connection);
		return selection;
	}


	public int updateIssueAssign(String selectionModifier)
	{
	    System.out.println("");
		System.out.println(assignedto+""+dev_id+""+issue_num);

		String updateSQL = "update issue set assignedto = '" + assignedto + "' , issue_status ='"+ issue_status +"', issue_priority="+issue_priority+ " where issue_num = "+issue_num+";";
		System.out.println(updateSQL);
		return DBAction.update(updateSQL);		
		
	}
	
	public int updateIssueResolve(int selectionModifier)
	{
	    System.out.println("in id  "+selectionModifier);
		System.out.println(assignedto+""+dev_id+""+issue_num);

		String updateSQL = "update issue set issue_status ='solved' where issue_num = "+selectionModifier+";";
		System.out.println(updateSQL);
		return DBAction.update(updateSQL);		
		
	}

	public static IssueData findOne(String selectionModifier) {
		ResultSet resultSet = null;
		String query = "select * from issue " + selectionModifier;
		
		Connection connection = DBAction.getConnection();
		resultSet = DBAction.readFromDB(query, connection);
		try {
			if (resultSet.next()) {
				
				IssueData issue = new IssueData();
				issue.issue_id = resultSet.getInt("issue_id");
				issue.issue_num = resultSet.getInt("issue_num");
				issue.issue_subject = resultSet.getString("issue_subject");
				issue.descp = resultSet.getString("issue_descp");
				issue.issue_priority=resultSet.getInt("issue_priority");
				issue.category = resultSet.getString("issue_category");
				issue.client_email = resultSet.getString("client_email");
				issue.client_phone_no = resultSet.getString("client_phone_no");
				issue.seq_qn = resultSet.getString("seq_qn");
				issue.seq_ans = resultSet.getString("seq_ans");
				issue.created_datetime = resultSet.getString("created_datetime");
				issue.assignedto = resultSet.getString("assignedto");
				DBAction.close(resultSet);
				DBAction.close(connection);
				return issue;
			}
		} catch (SQLException e) {
            MyLogAction.myCatch("Book.java", 43, e);
			e.printStackTrace();
		}
		DBAction.close(resultSet);
		DBAction.close(connection);
		return null;
	}

	public int getIssue_num() {
		return issue_num;
	}

	public void setIssue_num(int issue_num) {
		this.issue_num = issue_num;
	}

	public String getIssue_subject() {
		return issue_subject;
	}

	public void setIssue_subject(String issue_subject) {
		this.issue_subject = issue_subject;
	}

	public String getDescp() {
		return descp;
	}

	public void setDescp(String descp) {
		this.descp = descp;
	}

	public String getAssignedto() {
		return assignedto;
	}

	public void setAssignedto(String assignedto) {
		this.assignedto = assignedto;
	}

	public String getCategory() {
		return category;
	}

	public void setCategory(String category) {
		this.category = category;
	}

	public String getClient_email() {
		return client_email;
	}

	public void setClient_email(String client_email) {
		this.client_email = client_email;
	}

	public String getClient_phone_no() {
		return client_phone_no;
	}

	public void setClient_phone_no(String client_phone_no) {
		this.client_phone_no = client_phone_no;
	}

	public String getSeq_qn() {
		return seq_qn;
	}

	public void setSeq_qn(String seq_qn) {
		this.seq_qn = seq_qn;
	}

	public String getSeq_ans() {
		return seq_ans;
	}

	public void setSeq_ans(String seq_ans) {
		this.seq_ans = seq_ans;
	}

	public String getCreated_datetime() {
		return created_datetime;
	}

	public void setCreated_datetime(String created_datetime) {
		this.created_datetime = created_datetime;
	}
	
}
